home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / cvt.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  54KB  |  1,859 lines

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988, 1992, 1993, 1995 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22.  
  23. /* This file contains the functions for converting C expressions
  24.    to different data types.  The only entry point is `convert'.
  25.    Every language front end must have a `convert' function
  26.    but what kind of conversions it does will depend on the language.  */
  27.  
  28. #include "config.h"
  29. #include "tree.h"
  30. #include "flags.h"
  31. #include "cp-tree.h"
  32. #include "class.h"
  33. #include "convert.h"
  34.  
  35. #undef NULL
  36. #define NULL (char *)0
  37.  
  38. /* Change of width--truncation and extension of integers or reals--
  39.    is represented with NOP_EXPR.  Proper functioning of many things
  40.    assumes that no other conversions can be NOP_EXPRs.
  41.  
  42.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  43.    Converting integer to real uses FLOAT_EXPR
  44.    and real to integer uses FIX_TRUNC_EXPR.
  45.  
  46.    Here is a list of all the functions that assume that widening and
  47.    narrowing is always done with a NOP_EXPR:
  48.      In convert.c, convert_to_integer.
  49.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  50.         and truthvalue_conversion.
  51.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  52.      In fold-const.c: fold.
  53.      In tree.c: get_narrower and get_unwidened.
  54.  
  55.    C++: in multiple-inheritance, converting between pointers may involve
  56.    adjusting them by a delta stored within the class definition.  */
  57.  
  58. /* Subroutines of `convert'.  */
  59.  
  60. /* Build a thunk.  What it is, is an entry point that when called will
  61.    adjust the this pointer (the first argument) by offset, and then
  62.    goto the real address of the function given by REAL_ADDR that we
  63.    would like called.  What we return is the address of the thunk.  */
  64. static tree
  65. build_thunk (offset, real_addr)
  66.      tree offset, real_addr;
  67. {
  68.   if (TREE_CODE (real_addr) != ADDR_EXPR
  69.       || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
  70.     {
  71.       sorry ("MI pointer to member conversion too complex");
  72.       return error_mark_node;
  73.     }
  74.   sorry ("MI pointer to member conversion too complex");
  75.   return error_mark_node;
  76. }
  77.  
  78. /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
  79.    another `pointer to method'.  This may involved the creation of
  80.    a thunk to handle the this offset calculation.  */
  81. static tree
  82. convert_fn_ptr (type, expr)
  83.      tree type, expr;
  84. {
  85.   if (flag_vtable_thunks)
  86.     {
  87.       tree intype = TREE_TYPE (expr);
  88.       tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
  89.                   TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
  90.       if (binfo == error_mark_node)
  91.     {
  92.       error ("  in pointer to member conversion");
  93.       return error_mark_node;
  94.     }
  95.       if (binfo == NULL_TREE)
  96.     {
  97.       /* ARM 4.8 restriction. */
  98.       error ("invalid pointer to member conversion");
  99.       return error_mark_node;
  100.     }
  101.  
  102.       if (BINFO_OFFSET_ZEROP (binfo))
  103.     return build1 (NOP_EXPR, type, expr);
  104.       return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
  105.     }
  106.   else
  107.     return build_ptrmemfunc (type, expr, 1);
  108. }
  109.  
  110. /* if converting pointer to pointer
  111.      if dealing with classes, check for derived->base or vice versa
  112.      else if dealing with method pointers, delegate
  113.      else convert blindly
  114.    else if converting class, pass off to build_type_conversion
  115.    else try C-style pointer conversion  */
  116. static tree
  117. cp_convert_to_pointer (type, expr)
  118.      tree type, expr;
  119. {
  120.   register tree intype = TREE_TYPE (expr);
  121.   register enum tree_code form;
  122.  
  123.   if (TYPE_PTRMEMFUNC_P (type))
  124.     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
  125.   if (TYPE_PTRMEMFUNC_P (intype))
  126.     intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
  127.  
  128.   form = TREE_CODE (intype);
  129.  
  130.   if (form == POINTER_TYPE || form == REFERENCE_TYPE)
  131.     {
  132.       intype = TYPE_MAIN_VARIANT (intype);
  133.  
  134.       if (TYPE_MAIN_VARIANT (type) != intype
  135.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  136.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  137.     {
  138.       enum tree_code code = PLUS_EXPR;
  139.       tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
  140.       if (binfo == error_mark_node)
  141.         return error_mark_node;
  142.       if (binfo == NULL_TREE)
  143.         {
  144.           binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
  145.           if (binfo == error_mark_node)
  146.         return error_mark_node;
  147.           code = MINUS_EXPR;
  148.         }
  149.       if (binfo)
  150.         {
  151.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  152.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  153.           || ! BINFO_OFFSET_ZEROP (binfo))
  154.         {
  155.           /* Need to get the path we took.  */
  156.           tree path;
  157.  
  158.           if (code == PLUS_EXPR)
  159.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  160.           else
  161.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  162.           return build_vbase_path (code, type, expr, path, 0);
  163.         }
  164.         }
  165.     }
  166.       if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
  167.       && TREE_CODE (type) == POINTER_TYPE
  168.       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
  169.     return convert_fn_ptr (type, expr);
  170.  
  171.       if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
  172.       && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
  173.     {
  174.       tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
  175.       tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
  176.       tree binfo = get_binfo (b1, b2, 1);
  177.       if (binfo == NULL_TREE)
  178.         binfo = get_binfo (b2, b1, 1);
  179.       if (binfo == error_mark_node)
  180.         return error_mark_node;
  181.     }
  182.  
  183.       return build1 (NOP_EXPR, type, expr);
  184.     }
  185.  
  186.   my_friendly_assert (form != OFFSET_TYPE, 186);
  187.  
  188.   if (TYPE_LANG_SPECIFIC (intype)
  189.       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
  190.     return convert_to_pointer (type, build_optr_ref (expr));
  191.  
  192.   if (IS_AGGR_TYPE (intype))
  193.     {
  194.       tree rval;
  195.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  196.       if (rval)
  197.     {
  198.       if (rval == error_mark_node)
  199.         cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
  200.               expr, intype, type);
  201.       return rval;
  202.     }
  203.     }
  204.  
  205.   if (integer_zerop (expr))
  206.     {
  207.       if (type == TREE_TYPE (null_pointer_node))
  208.     return null_pointer_node;
  209.       expr = build_int_2 (0, 0);
  210.       TREE_TYPE (expr) = type;
  211.       return expr;
  212.     }
  213.  
  214.   if (INTEGRAL_CODE_P (form))
  215.     {
  216.       if (type_precision (intype) == POINTER_SIZE)
  217.     return build1 (CONVERT_EXPR, type, expr);
  218.       expr = convert (type_for_size (POINTER_SIZE, 0), expr);
  219.       /* Modes may be different but sizes should be the same.  */
  220.       if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
  221.       != GET_MODE_SIZE (TYPE_MODE (type)))
  222.     /* There is supposed to be some integral type
  223.        that is the same width as a pointer.  */
  224.     abort ();
  225.       return convert_to_pointer (type, expr);
  226.     }
  227.  
  228.   cp_error ("cannot convert `%E' from type `%T' to type `%T'",
  229.         expr, intype, type);
  230.   return error_mark_node;
  231. }
  232.  
  233. /* Like convert, except permit conversions to take place which
  234.    are not normally allowed due to access restrictions
  235.    (such as conversion from sub-type to private super-type).  */
  236. static tree
  237. convert_to_pointer_force (type, expr)
  238.      tree type, expr;
  239. {
  240.   register tree intype = TREE_TYPE (expr);
  241.   register enum tree_code form = TREE_CODE (intype);
  242.   
  243.   if (integer_zerop (expr))
  244.     {
  245.       if (type == TREE_TYPE (null_pointer_node))
  246.     return null_pointer_node;
  247.       expr = build_int_2 (0, 0);
  248.       TREE_TYPE (expr) = type;
  249.       return expr;
  250.     }
  251.  
  252.   /* Convert signature pointer/reference to `void *' first.  */
  253.   if (form == RECORD_TYPE
  254.       && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
  255.     {
  256.       expr = build_optr_ref (expr);
  257.       intype = TREE_TYPE (expr);
  258.       form = TREE_CODE (intype);
  259.     }
  260.  
  261.   if (form == POINTER_TYPE)
  262.     {
  263.       intype = TYPE_MAIN_VARIANT (intype);
  264.  
  265.       if (TYPE_MAIN_VARIANT (type) != intype
  266.       && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
  267.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
  268.     {
  269.       enum tree_code code = PLUS_EXPR;
  270.       tree path;
  271.       int distance = get_base_distance (TREE_TYPE (type),
  272.                         TREE_TYPE (intype), 0, &path);
  273.       if (distance == -2)
  274.         {
  275.         ambig:
  276.           cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
  277.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  278.           return error_mark_node;
  279.         }
  280.       if (distance == -1)
  281.         {
  282.           distance = get_base_distance (TREE_TYPE (intype),
  283.                         TREE_TYPE (type), 0, &path);
  284.           if (distance == -2)
  285.         goto ambig;
  286.           if (distance < 0)
  287.         /* Doesn't need any special help from us.  */
  288.         return build1 (NOP_EXPR, type, expr);
  289.  
  290.           code = MINUS_EXPR;
  291.         }
  292.       return build_vbase_path (code, type, expr, path, 0);
  293.     }
  294.       return build1 (NOP_EXPR, type, expr);
  295.     }
  296.  
  297.   return cp_convert_to_pointer (type, expr);
  298. }
  299.  
  300. /* We are passing something to a function which requires a reference.
  301.    The type we are interested in is in TYPE. The initial
  302.    value we have to begin with is in ARG.
  303.  
  304.    FLAGS controls how we manage access checking.
  305.    CHECKCONST controls if we report error messages on const subversion.  */
  306. static tree
  307. build_up_reference (type, arg, flags, checkconst)
  308.      tree type, arg;
  309.      int flags, checkconst;
  310. {
  311.   tree rval, targ;
  312.   int literal_flag = 0;
  313.   tree argtype = TREE_TYPE (arg);
  314.   tree target_type = TREE_TYPE (type);
  315.   tree binfo = NULL_TREE;
  316.  
  317.   my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
  318.   if ((flags & LOOKUP_PROTECT)
  319.       && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  320.       && IS_AGGR_TYPE (argtype)
  321.       && IS_AGGR_TYPE (target_type))
  322.     {
  323.       binfo = get_binfo (target_type, argtype, 1);
  324.       if (binfo == error_mark_node)
  325.     return error_mark_node;
  326.       if (binfo == NULL_TREE)
  327.     return error_not_base_type (target_type, argtype);
  328.     }
  329.  
  330.   /* Pass along const and volatile down into the type. */
  331.   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
  332.     target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
  333.                     TYPE_VOLATILE (type));
  334.   targ = arg;
  335.   if (TREE_CODE (targ) == SAVE_EXPR)
  336.     targ = TREE_OPERAND (targ, 0);
  337.   while (TREE_CODE (targ) == NOP_EXPR
  338.      && (TYPE_MAIN_VARIANT (argtype)
  339.          == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
  340.     targ = TREE_OPERAND (targ, 0);
  341.  
  342.   switch (TREE_CODE (targ))
  343.     {
  344.     case INDIRECT_REF:
  345.       /* This is a call to a constructor which did not know what it was
  346.      initializing until now: it needs to initialize a temporary.  */
  347.       if (TREE_HAS_CONSTRUCTOR (targ))
  348.     {
  349.       tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0), 1);
  350.       TREE_HAS_CONSTRUCTOR (targ) = 0;
  351.       return build_up_reference (type, temp, flags, 1);
  352.     }
  353.       /* Let &* cancel out to simplify resulting code.
  354.          Also, throw away intervening NOP_EXPRs.  */
  355.       arg = TREE_OPERAND (targ, 0);
  356.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
  357.       || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
  358.     arg = TREE_OPERAND (arg, 0);
  359.  
  360.       /* in doing a &*, we have to get rid of the const'ness on the pointer
  361.      value.  Haven't thought about volatile here.  Pointers come to mind
  362.      here.  */
  363.       if (TREE_READONLY (arg))
  364.     {
  365.       arg = copy_node (arg);
  366.       TREE_READONLY (arg) = 0;
  367.     }
  368.  
  369.       rval = build1 (CONVERT_EXPR, type, arg);
  370.       TREE_REFERENCE_EXPR (rval) = 1;
  371.  
  372.       /* propagate the const flag on something like:
  373.  
  374.      class Base {
  375.      public:
  376.        int foo;
  377.      };
  378.  
  379.       class Derived : public Base {
  380.       public:
  381.     int bar;
  382.       };
  383.  
  384.       void func(Base&);
  385.  
  386.       void func2(const Derived& d) {
  387.     func(d);
  388.       }
  389.  
  390.         on the d parameter.  The below could have been avoided, if the flags
  391.         were down in the tree, not sure why they are not.  (mrs) */
  392.       /* The below code may have to be propagated to other parts of this
  393.      switch.  */
  394.       if (TREE_READONLY (targ) && !TREE_READONLY (arg)
  395.       && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
  396.       && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
  397.       && (TYPE_READONLY (target_type) && checkconst))
  398.     {
  399.       arg = copy_node (arg);
  400.       TREE_READONLY (arg) = TREE_READONLY (targ);
  401.     }
  402.       literal_flag = TREE_CONSTANT (arg);
  403.  
  404.       goto done;
  405.  
  406.       /* Get this out of a register if we happened to be in one by accident.
  407.      Also, build up references to non-lvalues it we must.  */
  408.       /* For &x[y], return (&) x+y */
  409.     case ARRAY_REF:
  410.       if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
  411.     return error_mark_node;
  412.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
  413.                   TREE_OPERAND (targ, 1), 1);
  414.       TREE_TYPE (rval) = type;
  415.       if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
  416.       && staticp (TREE_OPERAND (targ, 0)))
  417.     TREE_CONSTANT (rval) = 1;
  418.       goto done;
  419.  
  420.     case SCOPE_REF:
  421.       /* Could be a reference to a static member.  */
  422.       {
  423.     tree field = TREE_OPERAND (targ, 1);
  424.     if (TREE_STATIC (field))
  425.       {
  426.         rval = build1 (ADDR_EXPR, type, field);
  427.         literal_flag = 1;
  428.         goto done;
  429.       }
  430.       }
  431.  
  432.       /* We should have farmed out member pointers above.  */
  433.       my_friendly_abort (188);
  434.  
  435.     case COMPONENT_REF:
  436.       rval = build_component_addr (targ, build_pointer_type (argtype),
  437.                    "attempt to make a reference to bit-field structure member `%s'");
  438.       TREE_TYPE (rval) = type;
  439.       literal_flag = staticp (TREE_OPERAND (targ, 0));
  440.  
  441.       goto done;
  442.  
  443.       /* Anything not already handled and not a true memory reference
  444.      needs to have a reference built up.  Do so silently for
  445.      things like integers and return values from function,
  446.      but complain if we need a reference to something declared
  447.      as `register'.  */
  448.  
  449.     case RESULT_DECL:
  450.       if (staticp (targ))
  451.     literal_flag = 1;
  452.       TREE_ADDRESSABLE (targ) = 1;
  453.       put_var_into_stack (targ);
  454.       break;
  455.  
  456.     case PARM_DECL:
  457. #if 0
  458.       if (targ == current_class_decl)
  459.     {
  460.       error ("address of `this' not available");
  461. /* #if 0 */      
  462.       /* This code makes the following core dump the compiler on a sun4,
  463.          if the code below is used.
  464.  
  465.          class e_decl;
  466.          class a_decl;
  467.          typedef a_decl* a_ref;
  468.  
  469.          class a_s {
  470.          public:
  471.            a_s();
  472.            void* append(a_ref& item);
  473.          };
  474.          class a_decl {
  475.          public:
  476.            a_decl (e_decl *parent);
  477.            a_s  generic_s;
  478.            a_s  decls;
  479.            e_decl* parent;
  480.          };
  481.  
  482.          class e_decl {
  483.          public:
  484.            e_decl();
  485.            a_s implementations;
  486.          };
  487.  
  488.          void foobar(void *);
  489.  
  490.          a_decl::a_decl(e_decl *parent) {
  491.            parent->implementations.append(this);
  492.          }
  493.        */
  494.  
  495.       TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
  496.       put_var_into_stack (targ);
  497.       break;
  498. /* #else */
  499.       return error_mark_node;
  500. /* #endif */      
  501.     }
  502. #endif
  503.       /* Fall through.  */
  504.     case VAR_DECL:
  505.     case CONST_DECL:
  506.       if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
  507.       && !DECL_ARTIFICIAL (targ))
  508.     cp_warning ("address needed to build reference for `%D', which is declared `register'",
  509.             targ);
  510.       else if (staticp (targ))
  511.     literal_flag = 1;
  512.  
  513.       TREE_ADDRESSABLE (targ) = 1;
  514.       put_var_into_stack (targ);
  515.       break;
  516.  
  517.     case COMPOUND_EXPR:
  518.       {
  519.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
  520.                           LOOKUP_PROTECT, checkconst);
  521.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
  522.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
  523.     return rval;
  524.       }
  525.  
  526.     case PREINCREMENT_EXPR:
  527.     case PREDECREMENT_EXPR:
  528.     case MODIFY_EXPR:
  529.     case INIT_EXPR:
  530.       {
  531.     tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
  532.                           LOOKUP_PROTECT, checkconst);
  533.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  534.     TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
  535.     return rval;
  536.       }
  537.  
  538.     case COND_EXPR:
  539.       return build (COND_EXPR, type,
  540.             TREE_OPERAND (targ, 0),
  541.             build_up_reference (type, TREE_OPERAND (targ, 1),
  542.                     LOOKUP_PROTECT, checkconst),
  543.             build_up_reference (type, TREE_OPERAND (targ, 2),
  544.                     LOOKUP_PROTECT, checkconst));
  545.  
  546.       /* Undo the folding... */
  547.     case MIN_EXPR:
  548.     case MAX_EXPR:
  549.       return build (COND_EXPR, type,
  550.             build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
  551.                boolean_type_node, TREE_OPERAND (targ, 0),
  552.                TREE_OPERAND (targ, 1)),
  553.             build_up_reference (type, TREE_OPERAND (targ, 0),
  554.                     LOOKUP_PROTECT, checkconst),
  555.             build_up_reference (type, TREE_OPERAND (targ, 1),
  556.                     LOOKUP_PROTECT, checkconst));
  557.  
  558.     case WITH_CLEANUP_EXPR:
  559.       return build (WITH_CLEANUP_EXPR, type,
  560.             build_up_reference (type, TREE_OPERAND (targ, 0),
  561.                     LOOKUP_PROTECT, checkconst),
  562.             0, TREE_OPERAND (targ, 2));
  563.  
  564.     case BIND_EXPR:
  565.       arg = TREE_OPERAND (targ, 1);
  566.       if (arg == NULL_TREE)
  567.     {
  568.       compiler_error ("({ ... }) expression not expanded when needed for reference");
  569.       return error_mark_node;
  570.     }
  571.       rval = build1 (ADDR_EXPR, type, arg);
  572.       TREE_REFERENCE_EXPR (rval) = 1;
  573.       return rval;
  574.  
  575.     default:
  576.       break;
  577.     }
  578.  
  579.   if (TREE_ADDRESSABLE (targ) == 0)
  580.     {
  581.       tree temp;
  582.  
  583.       if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  584.     {
  585.       temp = build_cplus_new (argtype, targ, 1);
  586.       if (TREE_CODE (temp) == WITH_CLEANUP_EXPR)
  587.         rval = build (WITH_CLEANUP_EXPR, type,
  588.               build1 (ADDR_EXPR, type, TREE_OPERAND (temp, 0)),
  589.               0, TREE_OPERAND (temp, 2));
  590.       else
  591.         rval = build1 (ADDR_EXPR, type, temp);
  592.       goto done;
  593.     }
  594.       else
  595.     {
  596.       temp = get_temp_name (argtype, 0);
  597.       if (toplevel_bindings_p ())
  598.         {
  599.           /* Give this new temp some rtl and initialize it.  */
  600.           DECL_INITIAL (temp) = targ;
  601.           TREE_STATIC (temp) = 1;
  602.           cp_finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
  603.           /* Do this after declaring it static.  */
  604.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  605.           TREE_TYPE (rval) = type;
  606.           literal_flag = TREE_CONSTANT (rval);
  607.           goto done;
  608.         }
  609.       else
  610.         {
  611.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  612.           if (binfo && !BINFO_OFFSET_ZEROP (binfo))
  613.         rval = convert_pointer_to (target_type, rval);
  614.           else
  615.         TREE_TYPE (rval) = type;
  616.  
  617.           temp = build (MODIFY_EXPR, argtype, temp, arg);
  618.           TREE_SIDE_EFFECTS (temp) = 1;
  619.           return build (COMPOUND_EXPR, type, temp, rval);
  620.         }
  621.     }
  622.     }
  623.   else
  624.     rval = build1 (ADDR_EXPR, type, arg);
  625.  
  626.  done:
  627.   if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
  628.       || TYPE_USES_COMPLEX_INHERITANCE (target_type))
  629.     {
  630.       TREE_TYPE (rval) = build_pointer_type (argtype);
  631.       if (flags & LOOKUP_PROTECT)
  632.     rval = convert_pointer_to (target_type, rval);
  633.       else
  634.     rval
  635.       = convert_to_pointer_force (build_pointer_type (target_type), rval);
  636.       TREE_TYPE (rval) = type;
  637.       if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
  638.     TREE_TYPE (TREE_OPERAND (rval, 0))
  639.       = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
  640.     }
  641.   TREE_CONSTANT (rval) = literal_flag;
  642.   return rval;
  643. }
  644.  
  645. /* For C++: Only need to do one-level references, but cannot
  646.    get tripped up on signed/unsigned differences.
  647.  
  648.    DECL is either NULL_TREE or the _DECL node for a reference that is being
  649.    initialized.  It can be error_mark_node if we don't know the _DECL but
  650.    we know it's an initialization.  */
  651.  
  652. tree
  653. convert_to_reference (reftype, expr, convtype, flags, decl)
  654.      tree reftype, expr;
  655.      int convtype, flags;
  656.      tree decl;
  657. {
  658.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  659.   register tree intype = TREE_TYPE (expr);
  660.   tree rval = NULL_TREE;
  661.   tree rval_as_conversion = NULL_TREE;
  662.   int i;
  663.  
  664.   if (TREE_CODE (intype) == REFERENCE_TYPE)
  665.     my_friendly_abort (364);
  666.  
  667.   intype = TYPE_MAIN_VARIANT (intype);
  668.  
  669.   i = comp_target_types (type, intype, 0);
  670.  
  671.   if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
  672.       && ! (flags & LOOKUP_NO_CONVERSION))
  673.     {
  674.       /* Look for a user-defined conversion to lvalue that we can use.  */
  675.  
  676.       rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  677.  
  678.       if (rval_as_conversion && rval_as_conversion != error_mark_node
  679.       && real_lvalue_p (rval_as_conversion))
  680.     {
  681.       expr = rval_as_conversion;
  682.       rval_as_conversion = NULL_TREE;
  683.       intype = type;
  684.       i = 1;
  685.     }
  686.     }
  687.  
  688.   if (((convtype & CONV_STATIC) && i == -1)
  689.       || ((convtype & CONV_IMPLICIT) && i == 1))
  690.     {
  691.       if (flags & LOOKUP_COMPLAIN)
  692.     {
  693.       tree ttl = TREE_TYPE (reftype);
  694.       tree ttr;
  695.       
  696.       {
  697.         int r = TREE_READONLY (expr);
  698.         int v = TREE_THIS_VOLATILE (expr);
  699.         ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
  700.       }
  701.  
  702.       if (! real_lvalue_p (expr) &&
  703.           (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
  704.         {
  705.           if (decl)
  706.         /* Ensure semantics of [dcl.init.ref] */
  707.         cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
  708.                 reftype, intype);
  709.           else
  710.         cp_pedwarn ("conversion to `%T' from rvalue `%T'",
  711.                 reftype, intype);
  712.         }
  713.       else if (! (convtype & CONV_CONST))
  714.         {
  715.           if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
  716.         cp_pedwarn ("conversion from `%T' to `%T' discards const",
  717.                 ttr, reftype);
  718.           else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
  719.         cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
  720.                 ttr, reftype);
  721.         }
  722.     }
  723.  
  724.       return build_up_reference (reftype, expr, flags,
  725.                  ! (convtype & CONV_CONST));
  726.     }
  727.   else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
  728.     {
  729.       /* When casting an lvalue to a reference type, just convert into
  730.      a pointer to the new type and deference it.  This is allowed
  731.      by San Diego WP section 5.2.9 paragraph 12, though perhaps it
  732.      should be done directly (jason).  (int &)ri ---> *(int*)&ri */
  733.  
  734.       /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
  735.          meant.  */
  736.       if (TREE_CODE (intype) == POINTER_TYPE
  737.       && (comptypes (TREE_TYPE (intype), type, -1)))
  738.     cp_warning ("casting `%T' to `%T' does not dereference pointer",
  739.             intype, reftype);
  740.       
  741.       rval = build_unary_op (ADDR_EXPR, expr, 0);
  742.       if (rval != error_mark_node)
  743.     rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
  744.       if (rval != error_mark_node)
  745.     rval = build1 (NOP_EXPR, reftype, rval);
  746.     }
  747.   else if (decl)
  748.     {
  749.       tree rval_as_ctor = NULL_TREE;
  750.       
  751.       if (rval_as_conversion)
  752.     {
  753.       if (rval_as_conversion == error_mark_node)
  754.         {
  755.           cp_error ("conversion from `%T' to `%T' is ambiguous",
  756.             intype, reftype);
  757.           return error_mark_node;
  758.         }
  759.       rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
  760.                            flags, 1);
  761.     }
  762.       
  763.       /* Definitely need to go through a constructor here.  */
  764.       if (TYPE_HAS_CONSTRUCTOR (type)
  765.       && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
  766.       && (rval = build_method_call
  767.           (NULL_TREE, constructor_name_full (type),
  768.            build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
  769.            LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
  770.            | LOOKUP_ONLYCONVERTING)))
  771.     {
  772.       tree init;
  773.  
  774.       if (toplevel_bindings_p ())
  775.         {
  776.           extern tree static_aggregates;
  777.           tree t = get_temp_name (type, toplevel_bindings_p ());
  778.           init = build_method_call (t, constructor_name_full (type),
  779.                     build_tree_list (NULL_TREE, expr),
  780.                     TYPE_BINFO (type),
  781.                     LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
  782.                     | LOOKUP_ONLYCONVERTING);
  783.  
  784.           if (init == error_mark_node)
  785.         return error_mark_node;
  786.  
  787.           make_decl_rtl (t, NULL_PTR, 1);
  788.           static_aggregates = perm_tree_cons (expr, t, static_aggregates);
  789.           rval = build_unary_op (ADDR_EXPR, t, 0);
  790.         }
  791.       else
  792.         {
  793.           init = build_method_call (NULL_TREE, constructor_name_full (type),
  794.                     build_tree_list (NULL_TREE, expr),
  795.                     TYPE_BINFO (type),
  796.                     LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
  797.                     |LOOKUP_ONLYCONVERTING);
  798.  
  799.           if (init == error_mark_node)
  800.         return error_mark_node;
  801.  
  802.           rval = build_cplus_new (type, init, 1);
  803.           rval = build_up_reference (reftype, rval, flags, 1);
  804.         }
  805.       rval_as_ctor = rval;
  806.     }
  807.  
  808.       if (rval_as_ctor && rval_as_conversion)
  809.     {
  810.       cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
  811.             intype, reftype);
  812.       return error_mark_node;
  813.     }
  814.       else if (rval_as_ctor)
  815.     rval = rval_as_ctor;
  816.       else if (rval_as_conversion)
  817.     rval = rval_as_conversion;
  818.       else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
  819.     {
  820.       rval = convert (type, expr);
  821.       if (rval == error_mark_node)
  822.         return error_mark_node;
  823.       
  824.       rval = build_up_reference (reftype, rval, flags, 1);
  825.     }
  826.  
  827.       if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
  828.     cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
  829.             reftype, intype);
  830.     }
  831.  
  832.   if (rval)
  833.     {
  834.       /* If we found a way to convert earlier, then use it. */
  835.       return rval;
  836.     }
  837.  
  838.   my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
  839.  
  840.   if (flags & LOOKUP_COMPLAIN)
  841.     cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
  842.  
  843.   if (flags & LOOKUP_SPECULATIVELY)
  844.     return NULL_TREE;
  845.  
  846.   return error_mark_node;
  847. }
  848.  
  849. /* We are using a reference VAL for its value. Bash that reference all the
  850.    way down to its lowest form. */
  851. tree
  852. convert_from_reference (val)
  853.      tree val;
  854. {
  855.   tree type = TREE_TYPE (val);
  856.  
  857.   if (TREE_CODE (type) == OFFSET_TYPE)
  858.     type = TREE_TYPE (type);
  859.   if (TREE_CODE (type) == REFERENCE_TYPE)
  860.     return build_indirect_ref (val, NULL_PTR);
  861.   return val;
  862. }
  863.  
  864. /* See if there is a constructor of type TYPE which will convert
  865.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  866.    not worry about finding constructors for base classes, then converting
  867.    to the derived class.
  868.  
  869.    MSGP is a pointer to a message that would be an appropriate error
  870.    string.  If MSGP is NULL, then we are not interested in reporting
  871.    errors.  */
  872. tree
  873. convert_to_aggr (type, expr, msgp, protect)
  874.      tree type, expr;
  875.      char **msgp;
  876.      int protect;
  877. {
  878.   tree basetype = type;
  879.   tree name = TYPE_IDENTIFIER (basetype);
  880.   tree function, fndecl, fntype, parmtypes, parmlist, result;
  881.   tree method_name;
  882.   enum access_type access;
  883.   int can_be_private, can_be_protected;
  884.  
  885.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  886.     {
  887.       if (msgp)
  888.     *msgp = "type `%s' does not have a constructor";
  889.       return error_mark_node;
  890.     }
  891.  
  892.   access = access_public;
  893.   can_be_private = 0;
  894.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  895.  
  896.   parmlist = build_tree_list (NULL_TREE, expr);
  897.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  898.  
  899.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  900.     {
  901.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  902.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  903.     }
  904.  
  905.   /* The type of the first argument will be filled in inside the loop.  */
  906.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  907.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  908.  
  909. #if 0
  910.   method_name = build_decl_overload (name, parmtypes, 1);
  911.  
  912.   /* constructors are up front.  */
  913.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  914.   if (TYPE_HAS_DESTRUCTOR (basetype))
  915.     fndecl = DECL_CHAIN (fndecl);
  916.  
  917.   while (fndecl)
  918.     {
  919.       if (DECL_ASSEMBLER_NAME (fndecl) == method_name)
  920.     {
  921.       function = fndecl;
  922.       if (protect)
  923.         {
  924.           if (TREE_PRIVATE (fndecl))
  925.         {
  926.           can_be_private =
  927.             (basetype == current_class_type
  928.              || is_friend (basetype, current_function_decl)
  929.              || purpose_member (basetype, DECL_ACCESS (fndecl)));
  930.           if (! can_be_private)
  931.             goto found;
  932.         }
  933.           else if (TREE_PROTECTED (fndecl))
  934.         {
  935.           if (! can_be_protected)
  936.             goto found;
  937.         }
  938.         }
  939.       goto found_and_ok;
  940.     }
  941.       fndecl = DECL_CHAIN (fndecl);
  942.     }
  943. #endif
  944.  
  945.   /* No exact conversion was found.  See if an approximate
  946.      one will do.  */
  947.   fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  948.   if (TYPE_HAS_DESTRUCTOR (basetype))
  949.     fndecl = DECL_CHAIN (fndecl);
  950.  
  951.   {
  952.     int saw_private = 0;
  953.     int saw_protected = 0;
  954.     struct candidate *candidates =
  955.       (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
  956.     struct candidate *cp = candidates;
  957.  
  958.     while (fndecl)
  959.       {
  960.     function = fndecl;
  961.     cp->h_len = 2;
  962.     cp->harshness = (struct harshness_code *)
  963.       alloca (3 * sizeof (struct harshness_code));
  964.  
  965.     compute_conversion_costs (fndecl, parmlist, cp, 2);
  966.     if ((cp->h.code & EVIL_CODE) == 0)
  967.       {
  968.         cp->u.field = fndecl;
  969.         if (protect)
  970.           {
  971.         if (TREE_PRIVATE (fndecl))
  972.           access = access_private;
  973.         else if (TREE_PROTECTED (fndecl))
  974.           access = access_protected;
  975.         else
  976.           access = access_public;
  977.           }
  978.         else
  979.           access = access_public;
  980.  
  981.         if (access == access_private
  982.         ? (basetype == current_class_type
  983.            || is_friend (basetype, cp->function)
  984.            || purpose_member (basetype, DECL_ACCESS (fndecl)))
  985.         : access == access_protected
  986.         ? (can_be_protected
  987.            || purpose_member (basetype, DECL_ACCESS (fndecl)))
  988.         : 1)
  989.           {
  990.         if (cp->h.code <= TRIVIAL_CODE)
  991.           goto found_and_ok;
  992.         cp++;
  993.           }
  994.         else
  995.           {
  996.         if (access == access_private)
  997.           saw_private = 1;
  998.         else
  999.           saw_protected = 1;
  1000.           }
  1001.       }
  1002.     fndecl = DECL_CHAIN (fndecl);
  1003.       }
  1004.     if (cp - candidates)
  1005.       {
  1006.     /* Rank from worst to best.  Then cp will point to best one.
  1007.        Private fields have their bits flipped.  For unsigned
  1008.        numbers, this should make them look very large.
  1009.        If the best alternate has a (signed) negative value,
  1010.        then all we ever saw were private members.  */
  1011.     if (cp - candidates > 1)
  1012.       qsort (candidates,    /* char *base */
  1013.          cp - candidates, /* int nel */
  1014.          sizeof (struct candidate), /* int width */
  1015.          rank_for_overload); /* int (*compar)() */
  1016.  
  1017.     --cp;
  1018.     if (cp->h.code & EVIL_CODE)
  1019.       {
  1020.         if (msgp)
  1021.           *msgp = "ambiguous type conversion possible for `%s'";
  1022.         return error_mark_node;
  1023.       }
  1024.  
  1025.     function = cp->function;
  1026.     fndecl = cp->u.field;
  1027.     goto found_and_ok;
  1028.       }
  1029.     else if (msgp)
  1030.       {
  1031.     if (saw_private)
  1032.       if (saw_protected)
  1033.         *msgp = "only private and protected conversions apply";
  1034.       else
  1035.         *msgp = "only private conversions apply";
  1036.     else if (saw_protected)
  1037.       *msgp = "only protected conversions apply";
  1038.     else
  1039.       *msgp = "no appropriate conversion to type `%s'";
  1040.       }
  1041.     return error_mark_node;
  1042.   }
  1043.   /* NOTREACHED */
  1044.  
  1045.  found:
  1046.   if (access == access_private)
  1047.     if (! can_be_private)
  1048.       {
  1049.     if (msgp)
  1050.       *msgp = TREE_PRIVATE (fndecl)
  1051.         ? "conversion to type `%s' is private"
  1052.         : "conversion to type `%s' is from private base class";
  1053.     return error_mark_node;
  1054.       }
  1055.   if (access == access_protected)
  1056.     if (! can_be_protected)
  1057.       {
  1058.     if (msgp)
  1059.       *msgp = TREE_PRIVATE (fndecl)
  1060.         ? "conversion to type `%s' is protected"
  1061.         : "conversion to type `%s' is from protected base class";
  1062.     return error_mark_node;
  1063.       }
  1064.   function = fndecl;
  1065.  found_and_ok:
  1066.  
  1067.   /* It will convert, but we don't do anything about it yet.  */
  1068.   if (msgp == 0)
  1069.     return NULL_TREE;
  1070.  
  1071.   fntype = TREE_TYPE (function);
  1072.   function = default_conversion (function);
  1073.  
  1074.   result = build_nt (CALL_EXPR, function,
  1075.              convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
  1076.                     parmlist, NULL_TREE, LOOKUP_NORMAL),
  1077.              NULL_TREE);
  1078.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1079.   TREE_SIDE_EFFECTS (result) = 1;
  1080.   return result;
  1081. }
  1082.  
  1083. /* Call this when we know (for any reason) that expr is not, in fact,
  1084.    zero.  This routine is like convert_pointer_to, but it pays
  1085.    attention to which specific instance of what type we want to
  1086.    convert to.  This routine should eventually become
  1087.    convert_to_pointer after all references to convert_to_pointer
  1088.    are removed.  */
  1089. tree
  1090. convert_pointer_to_real (binfo, expr)
  1091.      tree binfo, expr;
  1092. {
  1093.   register tree intype = TREE_TYPE (expr);
  1094.   tree ptr_type;
  1095.   tree type, rval;
  1096.  
  1097.   if (TREE_CODE (binfo) == TREE_VEC)
  1098.     type = BINFO_TYPE (binfo);
  1099.   else if (IS_AGGR_TYPE (binfo))
  1100.     {
  1101.       type = binfo;
  1102.     }
  1103.   else
  1104.     {
  1105.       type = binfo;
  1106.       binfo = NULL_TREE;
  1107.     }
  1108.  
  1109.   ptr_type = build_pointer_type (type);
  1110.   if (ptr_type == TYPE_MAIN_VARIANT (intype))
  1111.     return expr;
  1112.  
  1113.   if (intype == error_mark_node)
  1114.     return error_mark_node;
  1115.  
  1116.   my_friendly_assert (!integer_zerop (expr), 191);
  1117.  
  1118.   if (TREE_CODE (type) == RECORD_TYPE
  1119.       && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
  1120.       && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1121.     {
  1122.       tree path;
  1123.       int distance
  1124.     = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
  1125.                  0, &path);
  1126.  
  1127.       /* This function shouldn't be called with unqualified arguments
  1128.      but if it is, give them an error message that they can read.  */
  1129.       if (distance < 0)
  1130.     {
  1131.       cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
  1132.             TREE_TYPE (intype), type);
  1133.  
  1134.       if (distance == -2)
  1135.         cp_error ("because `%T' is an ambiguous base class", type);
  1136.       return error_mark_node;
  1137.     }
  1138.  
  1139.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1140.     }
  1141.   rval = build1 (NOP_EXPR, ptr_type,
  1142.          TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
  1143.   TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
  1144.   return rval;
  1145. }
  1146.  
  1147. /* Call this when we know (for any reason) that expr is
  1148.    not, in fact, zero.  This routine gets a type out of the first
  1149.    argument and uses it to search for the type to convert to.  If there
  1150.    is more than one instance of that type in the expr, the conversion is
  1151.    ambiguous.  This routine should eventually go away, and all
  1152.    callers should use convert_to_pointer_real.  */
  1153. tree
  1154. convert_pointer_to (binfo, expr)
  1155.      tree binfo, expr;
  1156. {
  1157.   tree type;
  1158.  
  1159.   if (TREE_CODE (binfo) == TREE_VEC)
  1160.     type = BINFO_TYPE (binfo);
  1161.   else if (IS_AGGR_TYPE (binfo))
  1162.       type = binfo;
  1163.   else
  1164.       type = binfo;
  1165.   return convert_pointer_to_real (type, expr);
  1166. }
  1167.  
  1168. /* Same as above, but don't abort if we get an "ambiguous" baseclass.
  1169.    There's only one virtual baseclass we are looking for, and once
  1170.    we find one such virtual baseclass, we have found them all.  */
  1171.  
  1172. tree
  1173. convert_pointer_to_vbase (binfo, expr)
  1174.      tree binfo;
  1175.      tree expr;
  1176. {
  1177.   tree intype = TREE_TYPE (TREE_TYPE (expr));
  1178.   tree binfos = TYPE_BINFO_BASETYPES (intype);
  1179.   int i;
  1180.  
  1181.   for (i = TREE_VEC_LENGTH (binfos)-1; i >= 0; i--)
  1182.     {
  1183.       tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
  1184.       if (BINFO_TYPE (binfo) == basetype)
  1185.     return convert_pointer_to (binfo, expr);
  1186.       if (binfo_member (BINFO_TYPE (binfo), CLASSTYPE_VBASECLASSES (basetype)))
  1187.     return convert_pointer_to_vbase (binfo, convert_pointer_to (basetype, expr));
  1188.     }
  1189.   my_friendly_abort (6);
  1190.   /* NOTREACHED */
  1191.   return NULL_TREE;
  1192. }
  1193.  
  1194. /* Conversion...
  1195.  
  1196.    FLAGS indicates how we should behave.  */
  1197.  
  1198. tree
  1199. cp_convert (type, expr, convtype, flags)
  1200.      tree type, expr;
  1201.      int convtype, flags;
  1202. {
  1203.   register tree e = expr;
  1204.   register enum tree_code code = TREE_CODE (type);
  1205.  
  1206.   if (TREE_CODE (e) == ERROR_MARK
  1207.       || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
  1208.     return error_mark_node;
  1209.  
  1210.   if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
  1211.     /* We need a new temporary; don't take this shortcut.  */;
  1212.   else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
  1213.     /* Trivial conversion: cv-qualifiers do not matter on rvalues.  */
  1214.     return fold (build1 (NOP_EXPR, type, e));
  1215.   
  1216.   if (code == VOID_TYPE && (convtype & CONV_STATIC))
  1217.     return build1 (CONVERT_EXPR, type, e);
  1218.  
  1219. #if 0
  1220.   /* This is incorrect.  A truncation can't be stripped this way.
  1221.      Extensions will be stripped by the use of get_unwidened.  */
  1222.   if (TREE_CODE (e) == NOP_EXPR)
  1223.     return convert (type, TREE_OPERAND (e, 0));
  1224. #endif
  1225.  
  1226.   /* Just convert to the type of the member.  */
  1227.   if (code == OFFSET_TYPE)
  1228.     {
  1229.       type = TREE_TYPE (type);
  1230.       code = TREE_CODE (type);
  1231.     }
  1232.  
  1233. #if 0
  1234.   if (code == REFERENCE_TYPE)
  1235.     return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
  1236.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1237.     e = convert_from_reference (e);
  1238. #endif
  1239.  
  1240.   if (TREE_CODE (e) == OFFSET_REF)
  1241.     e = resolve_offset_ref (e);
  1242.  
  1243.   if (TREE_READONLY_DECL_P (e))
  1244.     e = decl_constant_value (e);
  1245.  
  1246.   if (INTEGRAL_CODE_P (code))
  1247.     {
  1248.       tree intype = TREE_TYPE (e);
  1249.       enum tree_code form = TREE_CODE (intype);
  1250.       /* enum = enum, enum = int, enum = float are all errors. */
  1251.       if (flag_int_enum_equivalence == 0
  1252.       && TREE_CODE (type) == ENUMERAL_TYPE
  1253.       && ARITHMETIC_TYPE_P (intype)
  1254.       && ! (convtype & CONV_STATIC))
  1255.     {
  1256.       cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
  1257.  
  1258.       if (flag_pedantic_errors)
  1259.         return error_mark_node;
  1260.     }
  1261.       if (IS_AGGR_TYPE (intype))
  1262.     {
  1263.       tree rval;
  1264.       rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1265.       if (rval)
  1266.         return rval;
  1267.       if (flags & LOOKUP_COMPLAIN)
  1268.         cp_error ("`%#T' used where a `%T' was expected", intype, type);
  1269.       if (flags & LOOKUP_SPECULATIVELY)
  1270.         return NULL_TREE;
  1271.       return error_mark_node;
  1272.     }
  1273.       if (code == BOOLEAN_TYPE)
  1274.     return truthvalue_conversion (e);
  1275.       return fold (convert_to_integer (type, e));
  1276.     }
  1277.   if (code == POINTER_TYPE || code == REFERENCE_TYPE
  1278.       || TYPE_PTRMEMFUNC_P (type))
  1279.     return fold (cp_convert_to_pointer (type, e));
  1280.   if (code == REAL_TYPE)
  1281.     {
  1282.       if (IS_AGGR_TYPE (TREE_TYPE (e)))
  1283.     {
  1284.       tree rval;
  1285.       rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1286.       if (rval)
  1287.         return rval;
  1288.       else
  1289.         if (flags & LOOKUP_COMPLAIN)
  1290.           cp_error ("`%#T' used where a floating point value was expected",
  1291.             TREE_TYPE (e));
  1292.     }
  1293.       return fold (convert_to_real (type, e));
  1294.     }
  1295.  
  1296.   /* New C++ semantics:  since assignment is now based on
  1297.      memberwise copying,  if the rhs type is derived from the
  1298.      lhs type, then we may still do a conversion.  */
  1299.   if (IS_AGGR_TYPE_CODE (code))
  1300.     {
  1301.       tree dtype = TREE_TYPE (e);
  1302.       tree ctor = NULL_TREE;
  1303.       tree conversion = NULL_TREE;
  1304.  
  1305.       dtype = TYPE_MAIN_VARIANT (dtype);
  1306.  
  1307.       /* Conversion of object pointers or signature pointers/references
  1308.      to signature pointers/references.  */
  1309.  
  1310.       if (TYPE_LANG_SPECIFIC (type)
  1311.       && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
  1312.     {
  1313.       tree constructor = build_signature_pointer_constructor (type, expr);
  1314.       tree sig_ty = SIGNATURE_TYPE (type);
  1315.       tree sig_ptr;
  1316.  
  1317.       if (constructor == error_mark_node)
  1318.         return error_mark_node;
  1319.  
  1320.       sig_ptr = get_temp_name (type, 1);
  1321.       DECL_INITIAL (sig_ptr) = constructor;
  1322.       CLEAR_SIGNATURE (sig_ty);
  1323.       cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
  1324.       SET_SIGNATURE (sig_ty);
  1325.       TREE_READONLY (sig_ptr) = 1;
  1326.  
  1327.       return sig_ptr;
  1328.     }
  1329.  
  1330.       /* Conversion between aggregate types.  New C++ semantics allow
  1331.      objects of derived type to be cast to objects of base type.
  1332.      Old semantics only allowed this between pointers.
  1333.  
  1334.      There may be some ambiguity between using a constructor
  1335.      vs. using a type conversion operator when both apply.  */
  1336.  
  1337.       if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
  1338.       && TYPE_HAS_CONVERSION (dtype))
  1339.     conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
  1340.  
  1341.       if (conversion == error_mark_node)
  1342.     {
  1343.       if (flags & LOOKUP_COMPLAIN)
  1344.         error ("ambiguous pointer conversion");
  1345.       return conversion;
  1346.     }
  1347.  
  1348.       if (TYPE_HAS_CONSTRUCTOR (type))
  1349.     ctor = build_method_call (NULL_TREE, constructor_name_full (type),
  1350.                   build_tree_list (NULL_TREE, e),
  1351.                   TYPE_BINFO (type),
  1352.                   (flags & LOOKUP_NORMAL) | LOOKUP_SPECULATIVELY
  1353.                   | (convtype&CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
  1354.                   | (conversion ? LOOKUP_NO_CONVERSION : 0));
  1355.  
  1356.       if (ctor == error_mark_node)
  1357.     {
  1358.       if (flags & LOOKUP_COMPLAIN)
  1359.         cp_error ("in conversion to type `%T'", type);
  1360.       if (flags & LOOKUP_SPECULATIVELY)
  1361.         return NULL_TREE;
  1362.       return error_mark_node;
  1363.     }
  1364.       
  1365.       if (conversion && ctor)
  1366.     {
  1367.       if (flags & LOOKUP_COMPLAIN)
  1368.         error ("both constructor and type conversion operator apply");
  1369.       if (flags & LOOKUP_SPECULATIVELY)
  1370.         return NULL_TREE;
  1371.       return error_mark_node;
  1372.     }
  1373.       else if (conversion)
  1374.     return conversion;
  1375.       else if (ctor)
  1376.     {
  1377.       if (current_function_decl)
  1378.         /* We can't pass 1 to the with_cleanup_p arg here, because that
  1379.            screws up passing classes by value.  */
  1380.         ctor = build_cplus_new (type, ctor, 0);
  1381.       else
  1382.         {
  1383.           register tree parm = TREE_OPERAND (ctor, 1);
  1384.  
  1385.           /* Initializers for static variables and parameters
  1386.          have to handle doing the initialization and
  1387.          cleanup themselves.  */
  1388.           my_friendly_assert (TREE_CODE (ctor) == CALL_EXPR, 322);
  1389. #if 0
  1390.           /* The following assertion fails in cases where we
  1391.          are initializing a static member variable of a
  1392.          particular instance of a template class with a
  1393.          call to a constructor of the given instance, as
  1394.          in:
  1395.          
  1396.          TMPL<int> object = TMPL<int>();
  1397.          
  1398.          Curiously, the assertion does not fail if we do
  1399.          the same thing for a static member of a
  1400.          non-template class, as in:
  1401.          
  1402.          T object = T();
  1403.          
  1404.          I can't see why we should care here whether or not
  1405.          the initializer expression involves a call to
  1406.          `new', so for the time being, it seems best to
  1407.          just avoid doing this assertion.  */
  1408.           my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)),
  1409.                   323);
  1410. #endif
  1411.           TREE_VALUE (parm) = NULL_TREE;
  1412.           ctor = build_indirect_ref (ctor, NULL_PTR);
  1413.           TREE_HAS_CONSTRUCTOR (ctor) = 1;
  1414.         }
  1415.       return ctor;
  1416.     }
  1417.     }
  1418.  
  1419.   /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
  1420.      then the it won't be hashed and hence compare as not equal,
  1421.      even when it is.  */
  1422.   if (code == ARRAY_TYPE
  1423.       && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
  1424.       && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
  1425.     return e;
  1426.  
  1427.   if (flags & LOOKUP_COMPLAIN)
  1428.     cp_error ("conversion from `%T' to non-scalar type `%T' requested",
  1429.           TREE_TYPE (expr), type);
  1430.   if (flags & LOOKUP_SPECULATIVELY)
  1431.     return NULL_TREE;
  1432.   return error_mark_node;
  1433. }
  1434.  
  1435. /* Create an expression whose value is that of EXPR,
  1436.    converted to type TYPE.  The TREE_TYPE of the value
  1437.    is always TYPE.  This function implements all reasonable
  1438.    conversions; callers should filter out those that are
  1439.    not permitted by the language being compiled.  */
  1440.  
  1441. tree
  1442. convert (type, expr)
  1443.      tree type, expr;
  1444. {
  1445.   return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
  1446. }
  1447.  
  1448. /* Like convert, except permit conversions to take place which
  1449.    are not normally allowed due to access restrictions
  1450.    (such as conversion from sub-type to private super-type).  */
  1451. tree
  1452. convert_force (type, expr, convtype)
  1453.      tree type;
  1454.      tree expr;
  1455.      int convtype;
  1456. {
  1457.   register tree e = expr;
  1458.   register enum tree_code code = TREE_CODE (type);
  1459.  
  1460.   if (code == REFERENCE_TYPE)
  1461.     return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
  1462.                        NULL_TREE));
  1463.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1464.     e = convert_from_reference (e);
  1465.  
  1466.   if (code == POINTER_TYPE)
  1467.     return fold (convert_to_pointer_force (type, e));
  1468.  
  1469.   /* From typeck.c convert_for_assignment */
  1470.   if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
  1471.     && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
  1472.     && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
  1473.        || integer_zerop (e)
  1474.        || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
  1475.       && TYPE_PTRMEMFUNC_P (type))
  1476.     {
  1477.       /* compatible pointer to member functions. */
  1478.       return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
  1479.     }
  1480.  
  1481.   return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
  1482. }
  1483.  
  1484. /* Subroutine of build_type_conversion.  */
  1485. static tree
  1486. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1487.      tree xtype, basetype;
  1488.      tree expr;
  1489.      tree typename;
  1490.      int for_sure;
  1491. {
  1492.   tree rval;
  1493.   int flags;
  1494.  
  1495.   if (for_sure == 0)
  1496.     flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
  1497.   else
  1498.     flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
  1499.  
  1500.   rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
  1501.   if (rval == error_mark_node)
  1502.     {
  1503.       if (for_sure == 0)
  1504.     return NULL_TREE;
  1505.       return error_mark_node;
  1506.     }
  1507.  
  1508.   if (IS_AGGR_TYPE (TREE_TYPE (rval)))
  1509.     return rval;
  1510.  
  1511.   if (warn_cast_qual
  1512.       && TREE_TYPE (xtype)
  1513.       && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
  1514.       > TREE_READONLY (TREE_TYPE (xtype))))
  1515.     warning ("user-defined conversion casting away `const'");
  1516.   return convert (xtype, rval);
  1517. }
  1518.  
  1519. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1520.    exists, return the attempted conversion.  This may
  1521.    return ERROR_MARK_NODE if the conversion is not
  1522.    allowed (references private members, etc).
  1523.    If no conversion exists, NULL_TREE is returned.
  1524.  
  1525.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1526.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1527.    which can be evaluated if the results are ever needed.  */
  1528.  
  1529. tree
  1530. build_type_conversion (code, xtype, expr, for_sure)
  1531.      enum tree_code code;
  1532.      tree xtype, expr;
  1533.      int for_sure;
  1534. {
  1535.   /* C++: check to see if we can convert this aggregate type
  1536.      into the required type.  */
  1537.   tree basetype;
  1538.   tree conv;
  1539.   tree winner = NULL_TREE;
  1540.  
  1541.   if (expr == error_mark_node)
  1542.     return error_mark_node;
  1543.  
  1544.   basetype = TREE_TYPE (expr);
  1545.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1546.     basetype = TREE_TYPE (basetype);
  1547.  
  1548.   basetype = TYPE_MAIN_VARIANT (basetype);
  1549.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1550.     return NULL_TREE;
  1551.  
  1552.   /* Do we have an exact match?  */
  1553.   {
  1554.     tree typename = build_typename_overload (xtype);
  1555.     if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
  1556.       return build_type_conversion_1 (xtype, basetype, expr, typename,
  1557.                       for_sure);
  1558.   }
  1559.  
  1560.   /* Nope; try looking for others.  */
  1561.   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
  1562.     {
  1563.       if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
  1564.     continue;
  1565.  
  1566.       if (can_convert (xtype, TREE_VALUE (conv)))
  1567.     {
  1568.       if (winner)
  1569.         {
  1570.           if (for_sure)
  1571.         {
  1572.           cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
  1573.                 xtype);
  1574.           cp_error ("  candidate conversions include `%T' and `%T'",
  1575.                 TREE_VALUE (winner), TREE_VALUE (conv));
  1576.         }
  1577.           return NULL_TREE;
  1578.         }
  1579.       else
  1580.         winner = conv;
  1581.     }
  1582.     }
  1583.  
  1584.   if (winner)
  1585.     return build_type_conversion_1 (xtype, basetype, expr,
  1586.                     TREE_PURPOSE (winner), for_sure);
  1587.  
  1588.   return NULL_TREE;
  1589. }
  1590.  
  1591. /* Convert the given EXPR to one of a group of types suitable for use in an
  1592.    expression.  DESIRES is a combination of various WANT_* flags (q.v.)
  1593.    which indicates which types are suitable.  If COMPLAIN is 1, complain
  1594.    about ambiguity; otherwise, the caller will deal with it.  */
  1595.  
  1596. tree
  1597. build_expr_type_conversion (desires, expr, complain)
  1598.      int desires;
  1599.      tree expr;
  1600.      int complain;
  1601. {
  1602.   tree basetype = TREE_TYPE (expr);
  1603.   tree conv;
  1604.   tree winner = NULL_TREE;
  1605.  
  1606.   if (TREE_CODE (basetype) == OFFSET_TYPE)
  1607.     {
  1608.       expr = resolve_offset_ref (expr);
  1609.       basetype = TREE_TYPE (expr);
  1610.     }
  1611.  
  1612.   if (! IS_AGGR_TYPE (basetype))
  1613.     switch (TREE_CODE (basetype))
  1614.       {
  1615.       case INTEGER_TYPE:
  1616.     if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
  1617.         && integer_zerop (expr))
  1618.       return expr;
  1619.     /* else fall through... */
  1620.  
  1621.       case BOOLEAN_TYPE:
  1622.     return (desires & WANT_INT) ? expr : NULL_TREE;
  1623.       case ENUMERAL_TYPE:
  1624.     return (desires & WANT_ENUM) ? expr : NULL_TREE;
  1625.       case REAL_TYPE:
  1626.     return (desires & WANT_FLOAT) ? expr : NULL_TREE;
  1627.       case POINTER_TYPE:
  1628.     return (desires & WANT_POINTER) ? expr : NULL_TREE;
  1629.     
  1630.       case FUNCTION_TYPE:
  1631.       case ARRAY_TYPE:
  1632.     return (desires & WANT_POINTER) ? default_conversion (expr)
  1633.                                          : NULL_TREE;
  1634.       default:
  1635.     return NULL_TREE;
  1636.       }
  1637.  
  1638.   if (! TYPE_HAS_CONVERSION (basetype))
  1639.     return NULL_TREE;
  1640.  
  1641.   for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
  1642.     {
  1643.       int win = 0;
  1644.  
  1645.       if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
  1646.     continue;
  1647.  
  1648.       switch (TREE_CODE (TREE_VALUE (conv)))
  1649.     {
  1650.     case BOOLEAN_TYPE:
  1651.     case INTEGER_TYPE:
  1652.       win = (desires & WANT_INT); break;
  1653.     case ENUMERAL_TYPE:
  1654.       win = (desires & WANT_ENUM); break;
  1655.     case REAL_TYPE:
  1656.       win = (desires & WANT_FLOAT); break;
  1657.     case POINTER_TYPE:
  1658.       win = (desires & WANT_POINTER); break;
  1659.     }
  1660.  
  1661.       if (win)
  1662.     {
  1663.       if (winner)
  1664.         {
  1665.           if (complain)
  1666.         {
  1667.           cp_error ("ambiguous default type conversion from `%T'",
  1668.                 basetype);
  1669.           cp_error ("  candidate conversions include `%T' and `%T'",
  1670.                 TREE_VALUE (winner), TREE_VALUE (conv));
  1671.         }
  1672.           return error_mark_node;
  1673.         }
  1674.       else
  1675.         winner = conv;
  1676.     }
  1677.     }
  1678.  
  1679.   if (winner)
  1680.     return build_type_conversion_1 (TREE_VALUE (winner), basetype, expr,
  1681.                     TREE_PURPOSE (winner), 1);
  1682.  
  1683.   return NULL_TREE;
  1684. }
  1685.  
  1686. /* Must convert two aggregate types to non-aggregate type.
  1687.    Attempts to find a non-ambiguous, "best" type conversion.
  1688.  
  1689.    Return 1 on success, 0 on failure.
  1690.  
  1691.    @@ What are the real semantics of this supposed to be??? */
  1692. int
  1693. build_default_binary_type_conversion (code, arg1, arg2)
  1694.      enum tree_code code;
  1695.      tree *arg1, *arg2;
  1696. {
  1697.   switch (code)
  1698.     {
  1699.     case MULT_EXPR:
  1700.     case TRUNC_DIV_EXPR:
  1701.     case CEIL_DIV_EXPR:
  1702.     case FLOOR_DIV_EXPR:
  1703.     case ROUND_DIV_EXPR:
  1704.     case EXACT_DIV_EXPR:
  1705.       *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
  1706.       *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
  1707.       break;
  1708.  
  1709.     case TRUNC_MOD_EXPR:
  1710.     case FLOOR_MOD_EXPR:
  1711.     case LSHIFT_EXPR:
  1712.     case RSHIFT_EXPR:
  1713.     case BIT_AND_EXPR:
  1714.     case BIT_XOR_EXPR:
  1715.     case BIT_IOR_EXPR:
  1716.       *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
  1717.       *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
  1718.       break;
  1719.  
  1720.     case PLUS_EXPR:
  1721.       {
  1722.     tree a1, a2, p1, p2;
  1723.     int wins;
  1724.  
  1725.     a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
  1726.     a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
  1727.     p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
  1728.     p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
  1729.  
  1730.     wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
  1731.  
  1732.     if (wins > 1)
  1733.       error ("ambiguous default type conversion for `operator +'");
  1734.  
  1735.     if (a1 && a2)
  1736.       *arg1 = a1, *arg2 = a2;
  1737.     else if (a1 && p2)
  1738.       *arg1 = a1, *arg2 = p2;
  1739.     else
  1740.       *arg1 = p1, *arg2 = a2;
  1741.     break;
  1742.       }
  1743.  
  1744.     case MINUS_EXPR:
  1745.       {
  1746.     tree a1, a2, p1, p2;
  1747.     int wins;
  1748.  
  1749.     a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
  1750.     a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
  1751.     p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
  1752.     p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
  1753.  
  1754.     wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
  1755.  
  1756.     if (wins > 1)
  1757.       error ("ambiguous default type conversion for `operator -'");
  1758.  
  1759.     if (a1 && a2)
  1760.       *arg1 = a1, *arg2 = a2;
  1761.     else if (p1 && p2)
  1762.       *arg1 = p1, *arg2 = p2;
  1763.     else
  1764.       *arg1 = p1, *arg2 = a2;
  1765.     break;
  1766.       }
  1767.  
  1768.     case GT_EXPR:
  1769.     case LT_EXPR:
  1770.     case GE_EXPR:
  1771.     case LE_EXPR:
  1772.     case EQ_EXPR:
  1773.     case NE_EXPR:
  1774.       {
  1775.     tree a1, a2, p1, p2;
  1776.     int wins;
  1777.  
  1778.     a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
  1779.     a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
  1780.     p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
  1781.     p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
  1782.  
  1783.     wins = (a1 && a2) + (p1 && p2);
  1784.  
  1785.     if (wins > 1)
  1786.       cp_error ("ambiguous default type conversion for `%O'", code);
  1787.  
  1788.     if (a1 && a2)
  1789.       *arg1 = a1, *arg2 = a2;
  1790.     else
  1791.       *arg1 = p1, *arg2 = p2;
  1792.     break;
  1793.       }
  1794.  
  1795.     case TRUTH_ANDIF_EXPR:
  1796.     case TRUTH_ORIF_EXPR:
  1797.       *arg1 = convert (boolean_type_node, *arg1);
  1798.       *arg2 = convert (boolean_type_node, *arg2);
  1799.       break;
  1800.  
  1801.     default:
  1802.       *arg1 = NULL_TREE;
  1803.       *arg2 = NULL_TREE;
  1804.     }
  1805.  
  1806.   if (*arg1 == error_mark_node || *arg2 == error_mark_node)
  1807.     cp_error ("ambiguous default type conversion for `%O'", code);
  1808.  
  1809.   if (*arg1 && *arg2)
  1810.     return 1;
  1811.  
  1812.   return 0;
  1813. }
  1814.  
  1815. /* Implements integral promotion (4.1) and float->double promotion. */
  1816. tree
  1817. type_promotes_to (type)
  1818.      tree type;
  1819. {
  1820.   int constp = TYPE_READONLY (type);
  1821.   int volatilep = TYPE_VOLATILE (type);
  1822.   type = TYPE_MAIN_VARIANT (type);
  1823.  
  1824.   /* bool always promotes to int (not unsigned), even if it's the same
  1825.      size.  */
  1826.   if (type == boolean_type_node)
  1827.     type = integer_type_node;
  1828.  
  1829.   /* Normally convert enums to int, but convert wide enums to something
  1830.      wider.  */
  1831.   else if (TREE_CODE (type) == ENUMERAL_TYPE
  1832.        || type == wchar_type_node)
  1833.     {
  1834.       int precision = MAX (TYPE_PRECISION (type),
  1835.                TYPE_PRECISION (integer_type_node));
  1836.       tree totype = type_for_size (precision, 0);
  1837.       if (TREE_UNSIGNED (type)
  1838.       && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
  1839.     type = type_for_size (precision, 1);
  1840.       else
  1841.     type = totype;
  1842.     }
  1843.   else if (C_PROMOTING_INTEGER_TYPE_P (type))
  1844.     {
  1845.       /* Traditionally, unsignedness is preserved in default promotions.
  1846.          Otherwise, retain unsignedness if really not getting bigger.  */
  1847.       if (TREE_UNSIGNED (type)
  1848.       && (flag_traditional
  1849.           || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
  1850.     type = unsigned_type_node;
  1851.       else
  1852.     type = integer_type_node;
  1853.     }
  1854.   else if (type == float_type_node)
  1855.     type = double_type_node;
  1856.  
  1857.   return cp_build_type_variant (type, constp, volatilep);
  1858. }
  1859.